/**
 * @author GZY
 */
package sg.edu.nus.iss.team4ft.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import sg.edu.nus.iss.team4ft.service.Call;
import sg.edu.nus.iss.team4ft.service.FeatureCode;

public class AccountIOService {
	public AccountIOService(){}
	
	
	/**
	 * Returns a full list of call transactions. Refer to file data\call_txn_data.txt
	 * @return
	 * @throws IOException
	 * @throws NullPointerException
	 * @throws ParseException
	 */
	public static ArrayList<Call> getCallTransactions() throws IOException, NullPointerException, ParseException{
		String fileName = AppConstant.FILE_TRSCTN;
		ArrayList<Call> callTxns = new ArrayList<Call>();
		
		FileInputStream fis = new FileInputStream(fileName);
		InputStreamReader inReader = new InputStreamReader(fis);
		BufferedReader bufferedReader = new BufferedReader(inReader);
		String line = bufferedReader.readLine();
		line = bufferedReader.readLine();
		
		try{
			while (line!=null) {
				String[] splitInput = line.split(AppConstant.FILE_DELIMITER, -1);				
				callTxns.add(getCallTransaction(splitInput));				
				line = bufferedReader.readLine();
			}
		}finally{
			bufferedReader.close();
			inReader.close();
			fis.close();
		}
		return callTxns;
	}

	/**
	 * Creates a Call class from the line of transaction data
	 * @param splitInput
	 * @return
	 * @throws ParseException
	 */
	private static Call getCallTransaction(String[] splitInput) throws ParseException {
		Call callTxn = new Call();
		callTxn.setAccountNumber(splitInput[0]);
		callTxn.setTelephoneNumber(new Long(splitInput[1]));
		callTxn.setCallType(FeatureCode.valueOf(splitInput[2]));
		callTxn.setNumberCalled(Integer.parseInt(splitInput[3]));
		callTxn.setTimeOfCall(DateUtil.parse(splitInput[4], DateUtil.TXN_DATE_FORMAT));
		callTxn.setDurationOfCallInSeconds(Integer.parseInt(splitInput[5]));
		callTxn.setCalledNumber(splitInput[6]);
		
		return callTxn;
	}
	
	public static Map<String,String> subscriptionMap() throws IOException{
		String fileName = AppConstant.FILE_RATE;
		Map<String,String> subscription = new HashMap<String,String>();
		
		FileInputStream fis = new FileInputStream(fileName);
		InputStreamReader inReader = new InputStreamReader(fis);
		BufferedReader bufferedReader = new BufferedReader(inReader);
		String line = bufferedReader.readLine();
		line = bufferedReader.readLine();
		
		try{
			while (line!=null) {
				String[] splitInput = line.split(AppConstant.FILE_DELIMITER, -1);
				if(!splitInput[2].equalsIgnoreCase("")){
					String key = splitInput[0]+" "+splitInput[1];
					String value = splitInput[2];
					subscription.put(key, value);
				}
				line = bufferedReader.readLine();
			}
		}finally{
			bufferedReader.close();
			inReader.close();
			fis.close();
		}
		return subscription;
	}
	
	public static Map<String,String> usageChargeMap() throws IOException{
		String fileName = AppConstant.FILE_RATE;
		Map<String,String> usageCharge = new HashMap<String,String>();
		
		FileInputStream fis = new FileInputStream(fileName);
		InputStreamReader inReader = new InputStreamReader(fis);
		BufferedReader bufferedReader = new BufferedReader(inReader);
		String line = bufferedReader.readLine();
		line = bufferedReader.readLine();
		
		try{
			while (line!=null) {
				String[] splitInput = line.split(AppConstant.FILE_DELIMITER, -1);
				if(!splitInput[3].equalsIgnoreCase("")){
					String key = splitInput[0]+" "+splitInput[1];
					String value = splitInput[3];
					usageCharge.put(key, value);
				}
				line = bufferedReader.readLine();
			}
		}finally{
			bufferedReader.close();
			inReader.close();
			fis.close();
		}	
		return usageCharge;
	}

}
